package com.nerve.android.cloudoffice.app.activity.task;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Department;
import com.nerve.android.cloudoffice.domain.DepartmentTask;
import com.nerve.android.cloudoffice.service.NetService;

/**
 * @project: Cloudoffice_android
 * @file: DepartmentTaskActivity.java
 * @package: com.nerve.android.cloudoffice.app.activity.task
 * @description:
 *	查看部门中的任务
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 28, 2013
 * @change log:
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_task_of_department)
public class DepartmentTaskActivity extends NavigationActivity implements OnClickListener{
	
	@ViewOnId(id=R.id.headerGrid)
	private GridView gridView;
	
	private DepartmentTaskAdapter adapter;
	
	private List<DepartmentTask> dtList = new ArrayList<DepartmentTask>();
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			case WORKER_OK:
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};
	
	protected void initUI() {
		
		setNavTitle("查看部门任务");
		
		adapter = new DepartmentTaskAdapter();
		this.gridView.setAdapter(adapter);
		
		showProgressDialog(null, "正在加载所需数据...");
		new DepartmentTaskWorker().execute();
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.changeTab(IndexGroupActivity.HOME);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		DepartmentTask dt = dtList.get(id);
		
		Department d = new Department();
		d.setId(dt.getId());
		d.setName(dt.getName());
		
		putData(Const.SELECT_DEPARTMENT_ID, d);
		
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.next();
	}
	
	
	/**
	 * @project: Cloudoffice_android
	 * @file: DepartmentTaskActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity.task
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 28, 2013
	 * @change log:
	 */
	private class DepartmentTaskAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return dtList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return dtList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View v, ViewGroup arg2) {
			DepartmentTask dt = dtList.get(arg0);
			
			v = View.inflate(DepartmentTaskActivity.this, R.layout.activity_task_of_department_item, null);
			
			((TextView)v.findViewById(R.id.title)).setText(dt.getName());
			
			TextView counter = (TextView)v.findViewById(R.id.counter);
			if(dt.getTaskCount() == 0)
				counter.setVisibility(View.GONE);
			else{
				counter.setVisibility(View.VISIBLE);
				counter.setText(dt.getTaskCount() + "");
			}
			
			v.setId(arg0);
			v.setOnClickListener(DepartmentTaskActivity.this);
			
			return v;
		}
		
	}
	
	
	private class DepartmentTaskWorker extends AsyncTask<Void, Void, Integer>{

		@Override
		protected Integer doInBackground(Void... params) {
			Reply re = NetService.get(Const.HOST + "/department/countTask.htm");
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			try{
				dtList = JSON.parseArray(re.getMessage().toString(), DepartmentTask.class);
			}catch(Exception e){
				e.printStackTrace(System.out);
			}
			
			return WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}